package com.eliza.base.views.component.progress

import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Info
import androidx.compose.material3.Button
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavController
import kotlinx.coroutines.launch

object SnackBarX {
    @Composable
    fun Show(navController: NavController) {
        var swi by remember {
            mutableIntStateOf(0)
        }
        Column {
            Button(onClick = { swi = (swi + 1) % 2 }) {
                Text(text = "AppBarX ${swi}")
            }
            when (swi) {
                0 -> Simple()
                1 -> SimpleX()
            }
        }
    }

    @Composable
    private fun Simple() {
        val scope = rememberCoroutineScope()
        val snackbarHostState = remember { SnackbarHostState() }
        Scaffold(
            snackbarHost = {
                SnackbarHost(hostState = snackbarHostState)
            },
            floatingActionButton = {
                ExtendedFloatingActionButton(
                    text = { Text("Show snackbar") },
                    icon = { Icon(Icons.Filled.Info, contentDescription = "") },
                    onClick = {
                        scope.launch {
                            snackbarHostState.showSnackbar("Snackbar 内容 时长")
                        }
                    }
                )
            }
        ) { contentPadding ->
            // Screen content
            Column {
                Text(text = "${contentPadding.toString()}")
            }

        }
    }

    @Composable
    private fun SimpleX() {
        val scope = rememberCoroutineScope()
        val snackbarHostState = remember { SnackbarHostState() }
        val localContext = LocalContext.current

        Scaffold(
            snackbarHost = {
                SnackbarHost(hostState = snackbarHostState)
            },
            floatingActionButton = {
                ExtendedFloatingActionButton(
                    text = { Text("Show snackbar") },
                    icon = { Icon(Icons.Filled.Info, contentDescription = "") },
                    onClick = {
                        scope.launch {
                            val result = snackbarHostState.showSnackbar(
                                message = "Snackbar",
                                actionLabel = "Action",
                                // Defaults to SnackbarDuration.Short
                                duration = SnackbarDuration.Indefinite
                            )
                            when (result) {
                                SnackbarResult.ActionPerformed -> {
                                    Toast.makeText(
                                        localContext,
                                        "SnackbarResult.ActionPerformed",
                                        Toast.LENGTH_SHORT
                                    ).show()
                                }

                                SnackbarResult.Dismissed -> {
                                    /* Handle snackbar dismissed */
                                    Toast.makeText(
                                        localContext,
                                        "SnackbarResult.Dismissed",
                                        Toast.LENGTH_SHORT
                                    ).show()
                                }
                            }
                        }
                    }
                )
            }
        ) { contentPadding ->
            // Screen content
            Text(text = "${contentPadding.toString()}")

        }

    }
}
